package com.surprise.biz.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import redis.clients.jedis.Jedis;

import com.surprise.db.dao.CodeDAO;
import com.surprise.db.po.Code;
import com.surprise.redis.RedisFactory;
import com.surprise.redis.RedisKeyGenerator;

/**
 * CodeService.java
 * @author nick
 * @since 2015年9月6日
 */
@Service
public class CodeService {
	@Autowired
	private CodeDAO codeDAO;
	@Autowired
	private RedisFactory redisFactory;
	
	public void setCount(long couponId, int count) {
		String totalKey = RedisKeyGenerator.generateCouponCodeTotalKey(couponId);
		String countKey = RedisKeyGenerator.generateCouponCodeCountKey(couponId);
		Jedis jedis = null;
		try {
			jedis = redisFactory.getClient();
			jedis.set(totalKey, count+"");
			jedis.set(countKey, "0");
		} finally {
			redisFactory.returnResource(jedis);
		}
	}
	
//	/**
//	 * 
//	 * @param couponId
//	 * @return
//	 */
//	public int addCode(long couponId) {
//		
//		return 0;
//	}
	
	public List<Code> findByUserShop(String code, List<Long> shopids) {
		return codeDAO.findActiveByUserShop(code, shopids);
	}
	
	public int verify(long opid, long id) {
		return codeDAO.verify(opid, id);
	}
}
